﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace CarBlackBox
{
    public partial class Form_SystemOption : Form
    {
        bool bClose = true;
        public Form_SystemOption()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(cbCom.Text!=Properties.Settings.Default.DefaultPortName)
            {
                SerialCommunication.DefaultPortName = cbCom.Text;
                WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
                log.LogType = LogType.系统设置.ToString();
                log.LogContent = "系统默认串口由" + Properties.Settings.Default.DefaultPortName + "更改为" + cbCom.Text;
                log.LogRemark = "默认串口设置";
                Properties.Settings.Default.DefaultPortName = cbCom.Text;
                Properties.Settings.Default.Save();
                log.InserSQLLog();
            }
            this.DialogResult = DialogResult.OK;
        }

        private void Form_SystemOption_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            string str = Properties.Settings.Default.DefaultPortName;
            cbCom.SelectedItem = Properties.Settings.Default.DefaultPortName;
            if (cbCom.SelectedIndex == -1)
                cbCom.SelectedIndex = 0;
        }

        private void Form_SystemOption_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!bClose)
            {
                e.Cancel = true;
                bClose = true;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (WFNetLib.WFGlobal.MessageYseOrNo("确定取消当前操作么？", "产品信息编辑取消") == DialogResult.No)
                bClose = false;
        }
    }
}
